package org.rendao.biz.web;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.stream.StreamResult;

import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.rendao.auth.model.UserRole;
import org.rendao.biz.model.FirstIndex;
import org.rendao.biz.model.SearchResult;
import org.rendao.biz.service.FirstIndexService;
import org.rendao.exception.TBFException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.oxm.Marshaller;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

@Controller
public class SampleController {

	private static final Logger logger = Logger.getLogger(SampleController.class);
	
	@Autowired
	private FirstIndexService firstIndexService;
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@RequestMapping(value = "/count", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public String getCount() {
		Object result = sessionFactory.getCurrentSession().createSQLQuery("select count(*) from customers").uniqueResult();
		return result.toString();
	}
	
	@RequestMapping(value = "/insertFirstIndex", method = RequestMethod.GET)
	@ResponseBody
	public FirstIndex insertFirstIndex(@RequestParam("name") String name) throws Exception {
		FirstIndex firstIndex = new FirstIndex();
		firstIndex.setName(name);
		FirstIndex result = firstIndexService.saveOrUpdate(firstIndex);
		return result;
	}
	
	@RequestMapping(value = "/getFirstIndex", method = RequestMethod.GET)
	@ResponseBody
	public FirstIndex getFirstIndex(@RequestParam("id") String id) throws Exception {
		FirstIndex firstIndex = firstIndexService.getById(id);
		return firstIndex;
	}
	
	@RequestMapping(value = "/getFirstIndexName", method = RequestMethod.GET)
	@ResponseBody
	public String getFirstIndexName(@RequestParam("id") String id) throws Exception {
		FirstIndex firstIndex = firstIndexService.getById(id);
		List<String> result = new ArrayList<String>();
		result.add(firstIndex.getName());
		return firstIndex.getName();
	}
	
	@RequestMapping(value = "/report", method = RequestMethod.GET)
	public ModelAndView handleSimpleReportMulti(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();
//		model.put("format", "pdf");
		FirstIndex firstIndex = firstIndexService.getById("2");
		List<FirstIndex> list = new ArrayList<FirstIndex>();
		list.add(firstIndex);
		JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list, false);
		model.put("dataSource", dataSource);
		return new ModelAndView("simpleReport", model);
	}
	
	@RequestMapping(value = "/error", method = RequestMethod.GET)
	@ResponseBody
	public String handleError(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		throw new Exception("Test Error");
	}
	
	@RequestMapping(value = "/test_search_result", method = RequestMethod.GET)
	@ResponseBody
	public SearchResult<UserRole> getSampleSearchResult() throws Exception {
		UserRole role = new UserRole();
		role.setId("001");
		role.setName("dd");
		role.setIsDeleted("F");
		List<UserRole> roles = new ArrayList<UserRole>();
		roles.add(role);
		SearchResult<UserRole> result = SearchResult.byCollection(roles); 
		return result;
	}
}
